/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Attachment;
import com.guoyu.project.model.Banner;
import com.guoyu.project.service.AttachmentService;
import com.guoyu.project.service.FileUploadService;

@Controller
@RequestMapping("admin/attachment")
public class AttachmentController {

    private final static Logger logger = LoggerFactory.getLogger(AttachmentController.class);

    @Resource
    private AttachmentService attachmentService;

    @Resource
    private FileUploadService fileUploadService;

    @RequestMapping("index")
    public String index() {
        return "admin/attachment/index";
    }

   
    @RequestMapping("attachment_list")
    @ResponseBody
    public Pagination<Attachment> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        return attachmentService.queryAllAttachment(offset, limit, params);
    }

    /**
     * 跳到添加今日资讯填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        return "admin/attachment/add";
    }

    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "upattachmentfile", required = false) MultipartFile file, Attachment attachment) {
        logger.info("提交添加附件信息：{}", JsonUtil.encode(attachment));
        String attachmentPath = fileUploadService.upload(file);
        attachment.setAttachment_url(attachmentPath);
        attachment.setStatus(0);
        attachmentService.saveAttachment(attachment);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(Integer attachment_id, Model model) {
    	Attachment attachment = attachmentService.queryById(attachment_id);
        model.addAttribute("attachment", attachment);
        return "admin/attachment/edit";
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "attachmentfile", required = false) MultipartFile file, Attachment attachment) {
        logger.info("提交修改附件信息：{}", JsonUtil.encode(attachment));
        String filePath = StringUtils.defaultIfBlank(fileUploadService.upload(file), attachment.getAttachment_url());
        attachment.setAttachment_url(filePath);;
        attachmentService.updateAttachment(attachment);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除今日资讯信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(Integer attachment_id) {
    	attachmentService.deleteById(attachment_id);
        return APIResponse.returnSuccess();
    }

}